#!/usr/bin/env python
import os
import sys
import shutil
import time
from py2neo import *

gz_金 = ["庚","辛","申","酉"]
gz_木 = ["甲","乙","寅","卯"]
gz_水 = ["壬","癸","子","亥"]
gz_火 = ["丙","丁","巳","午"]
gz_土 = ["戊","己","丑","辰","未","戌"]



def getBeamIniMat(rscFilesDir,graph):
    '''
    获取ant目录下的所有天线矩阵
    :param rscFilesDir:
    :return:
    '''
    listFiles = os.listdir(rscFilesDir)
    try:
        for file in listFiles:
            tmpFileName = file.replace(".txt", "").replace(" ", "_").replace('\n', '').replace('\r', '')
            print("处理易理文件：{}".format(tmpFileName))
            with open(os.path.join(rscFilesDir,file)) as file_object:
                for tmp_line in file_object:
                    tmp_line=tmp_line.replace('\n', '').replace('\r', '')
                    if tmp_line.find('合') >= 0:
                        tmp_Node = Node('局', name=tmp_line,status="on",wuxing=tmp_line[tmp_line.find('合')+1:])
                        graph.create(tmp_Node)
                        tmp_ab=tmp_line[:tmp_line.find('合')]
                        tmp_len=len(tmp_ab)
                        for i in range(tmp_len):
                            tmp_a=tmp_ab[i]
                            print(tmp_a)
                            tmp_nodes = list(graph.nodes.match(name=tmp_a))
                            print(tmp_nodes)
                            if len(tmp_nodes) > 0:
                                for tmp_node in tmp_nodes:
                                    graph.create(Relationship(tmp_Node, "包含", tmp_node,status="on"))
                            else:
                                pass
                    if tmp_line.find('冲') >= 0:
                        tmp_ab = tmp_line[:tmp_line.find('冲')]
                        tmp_a = tmp_ab[0]
                        tmp_b = tmp_ab[1]
                        tmp_nodea = graph.nodes.match(name=tmp_a).first()
                        tmp_nodeb = graph.nodes.match(name=tmp_b).first()
                        graph.create(Relationship(tmp_nodea, "冲", tmp_nodeb,status="on"))
                        graph.create(Relationship(tmp_nodeb, "冲", tmp_nodea,status="on"))



                #exit(0)

    except Exception as e:
        print('error:{}!'.format(e))
        exit(1)
    return 0


if __name__ == '__main__':
    time1 = time.time()
    print('Programe starting...')
    graph = Graph("http://localhost:7474", auth=("neo4j", "123"))

    graph.run("MATCH (n) DETACH DELETE n")

    # 以下代码创建了两个节点，其中Person表示标签(label)，name和age是属性
    node_甲 = Node('干', name='甲',status="on")
    node_乙 = Node('干', name='乙',status="on")
    node_丙 = Node('干', name='丙',status="on")
    node_丁 = Node('干', name='丁',status="on")
    node_戊 = Node('干', name='戊',status="on")
    node_己 = Node('干', name='己',status="on")
    node_庚 = Node('干', name='庚',status="on")
    node_辛 = Node('干', name='辛',status="on")
    node_壬 = Node('干', name='壬',status="on")
    node_癸 = Node('干', name='癸',status="on")

    graph.create(node_甲)
    graph.create(node_乙)
    graph.create(node_丙)
    graph.create(node_丁)
    graph.create(node_戊)
    graph.create(node_己)
    graph.create(node_庚)
    graph.create(node_辛)
    graph.create(node_壬)
    graph.create(node_癸)

    node_子 = Node('支', name='子',status="on")
    node_丑= Node('支', name='丑',status="on")
    node_寅 = Node('支', name='寅',status="on")
    node_卯 = Node('支', name='卯',status="on")
    node_辰 = Node('支', name='辰',status="on")
    node_巳 = Node('支', name='巳',status="on")
    node_午 = Node('支', name='午',status="on")
    node_未 = Node('支', name='未',status="on")
    node_申 = Node('支', name='申',status="on")
    node_酉 = Node('支', name='酉',status="on")
    node_戌 = Node('支', name='戌',status="on")
    node_亥 = Node('支', name='亥',status="on")

    graph.create(node_子)
    graph.create(node_丑)
    graph.create(node_寅)
    graph.create(node_卯)
    graph.create(node_辰)
    graph.create(node_巳)
    graph.create(node_午)
    graph.create(node_未)
    graph.create(node_申)
    graph.create(node_酉)
    graph.create(node_戌)
    graph.create(node_亥)

    node_金 = Node('五行', name='金', status="on")
    node_木 = Node('五行', name='木', status="on")
    node_水 = Node('五行', name='水', status="on")
    node_火 = Node('五行', name='火', status="on")
    node_土 = Node('五行', name='土', status="on")
    graph.create(node_金)
    graph.create(node_木)
    graph.create(node_水)
    graph.create(node_火)
    graph.create(node_土)

    #根据指定的天线目录，把里面的beam的MSI文件转为初始的3D的增益gain矩阵
    MSIList = getBeamIniMat(os.path.join('易理'),graph)
    print('get 易理 files to success.')

    matcher = NodeMatcher(graph)
    tmpju_nodes = matcher.match("局")
    for tmp_node in tmpju_nodes:
        tmp_key = dict(tmp_node)
        juwuxing=tmp_key['wuxing']
        if juwuxing=='金':
            print(tmp_key['wuxing'])
            for tmp_node2 in gz_金:
                print(tmp_node2)
                graph.create(Relationship(tmp_node, "比", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_水:
                graph.create(Relationship(tmp_node, "生", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_木:
                graph.create(Relationship(tmp_node, "克", matcher.match(name=tmp_node2).first(), status="on"))
        elif juwuxing=='木':
            print(tmp_key['wuxing'])
            for tmp_node2 in gz_木:
                 graph.create(Relationship(tmp_node, "比", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_火:
                 graph.create(Relationship(tmp_node, "生", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_土:
                graph.create(Relationship(tmp_node, "克", matcher.match(name=tmp_node2).first(), status="on"))
        elif juwuxing=='水':
            print(tmp_key['wuxing'])
            for tmp_node2 in gz_水:
                 graph.create(Relationship(tmp_node, "比", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_木:
                 graph.create(Relationship(tmp_node, "生", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_火:
                graph.create(Relationship(tmp_node, "克", matcher.match(name=tmp_node2).first(), status="on"))
        elif juwuxing=='火':
            print(tmp_key['wuxing'])
            for tmp_node2 in gz_火:
                 graph.create(Relationship(tmp_node, "比", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_土:
                 graph.create(Relationship(tmp_node, "生", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_金:
                graph.create(Relationship(tmp_node, "克", matcher.match(name=tmp_node2).first(), status="on"))
        elif juwuxing=='土':
            print(tmp_key['wuxing'])
            for tmp_node2 in gz_土:
                 graph.create(Relationship(tmp_node, "比", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_金:
                 graph.create(Relationship(tmp_node, "生", matcher.match(name=tmp_node2).first(), status="on"))
            for tmp_node2 in gz_水:
                graph.create(Relationship(tmp_node, "克", matcher.match(name=tmp_node2).first(), status="on"))


# 打印用时
    time2 = time.time()
    print('total used {0} min.'.format(round((time2 - time1) / 60, 2)))
